package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingProjectDto;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.vo.NursingProjectVO;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        int i = save(nursingProject) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        int i = updateById(nursingProject) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingProjectById(Long id) {
        Long count = nursingProjectPlanMapper.selectCount(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getProjectId, id));
        if (count > 0L) {
            throw new ServiceException("该项目已关联护理计划，无法删除");
        }
        int i = removeById(id) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    @Override
    public List<NursingProjectVO> selectAll() {
        List<NursingProjectVO> nursingProjectVOS = (List<NursingProjectVO>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (CollectionUtils.isNotEmpty(nursingProjectVOS)) {
            return nursingProjectVOS;
        }
        nursingProjectVOS = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(CACHE_KEY, nursingProjectVOS);
        return nursingProjectVOS;
    }

    /**
     * 小程序分页查询护理项目
     *
     * @param dto
     * @return
     */
    @Override
    public TableDataInfo queryNursingProjectPage(NursingProjectDto dto) {
        IPage page = new Page<NursingProject>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<NursingProject> queryWrapper = Wrappers.<NursingProject>lambdaQuery()
                .eq(dto.getStatus() != null, NursingProject::getStatus, dto.getStatus())
                .like(dto.getName() != null, NursingProject::getName, dto.getName());
        page = page(page, queryWrapper);
        TableDataInfo tableDataInfo = new TableDataInfo<>(page.getRecords(), (int) page.getTotal());
        tableDataInfo.setCode(HttpServletResponse.SC_OK);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    /// 删除redis缓存
    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY);
    }
}
